package com.example.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.example.reggie.common.BaseContext;
import com.example.reggie.common.R;
import com.example.reggie.entity.ShoppingCart;
import com.example.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart/")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 将套餐、菜品加入购物车，并保存到购物车表
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        log.info("购物车数据:{}",shoppingCart);
        //设置用户id，指定当前是哪个用户的购物车数据
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //查询添加的菜品或者套餐是否在购物车中
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        if(shoppingCart.getSetmealId()!=null){
            //添加到购物车中的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }else if(shoppingCart.getDishId()!=null){
            //添加到购物车中的是菜品
            queryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }
        ShoppingCart sc = shoppingCartService.getOne(queryWrapper);
        if(sc!=null){
            //如果已经存在，就在原来的数量加一
            Integer number = sc.getNumber();
            sc.setNumber(number+1);
            shoppingCartService.updateById(sc);

        }else{
            //如果不存在，则添加到购物车，数量默认是一
            sc=new ShoppingCart();
            BeanUtils.copyProperties(shoppingCart,sc);
            sc.setNumber(1);
            shoppingCartService.save(sc);
        }


        return R.success(sc);
    }

    /**
     * 获取购物车列表信息
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        //获取当前用户id
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }


    /**
     * 减少购物车中菜品/套餐的数量
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        log.info("购物车数据:{}",shoppingCart);
        //设置用户id，指定当前是哪个用户的购物车数据
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //查询减少的菜品或者套餐是否在购物车中
        LambdaQueryWrapper<ShoppingCart> wrapper=new LambdaQueryWrapper<>();
        if (shoppingCart.getDishId()!=null){
            //是菜品
            wrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else{
            //是套餐
            wrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        //获得购物车数据
        ShoppingCart sc = shoppingCartService.getOne(wrapper);
        Integer number = sc.getNumber();
        if(number>1){
            sc.setNumber(number-1);
            shoppingCartService.updateById(sc);
        }else {
            //数量等于1则移除该物品
            shoppingCartService.removeById(sc.getId());
        }


        return R.success(sc);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        shoppingCartService.remove(queryWrapper);
        return R.success("清空购物车成功");
    }

}
